<?

if (null==$listfield1) $listfield1 =  "no";
if (null==$listfield2) $listfield2 = "pay_method";
if (null==$listfield3) $listfield3 = "member_name";
if (null==$listfield4) $listfield4 = "member_email";
if (null==$listfield5) $listfield5 = "purchase_date";
if (null==$listfield6) $listfield6 = "shipping_status";

//require("config.php");

$label = "Order List";

$table="cus_order";


if ($action=="search"){

	$ftime=strtotime($fy.'-'.$fm.'-'.$fd.' 00:00:00');
	$ttime=strtotime($ty.'-'.$tm.'-'.$td.' 23:59:59');

	if ($no) $nostr=' and no = '.$no;
	else $nostr='';

	if ($firstname) $namestr="%$firstname%";
	else $namestr='';
	if ($lastname) 	$namestr.="$lastname%";
	if ($namestr) 	$namestr=" and member_name like '$namestr'";
	
	if ($member_email) $emailstr=" and member_email like '%$member_email%'";
	else $emailstr='';
	
	if ($hkid) $hkidstr=" and hkid like'%$hkid%'";
	else $hkidstr='';
	
	$Query="SELECT 
	id, $listfield1, $listfield2, $listfield3,$listfield4, $listfield5, $listfield6,recipient_tel,order_status,delivery_date,billing_country,subtotal,shippingfee from $table
	 WHERE 
	purchase_date	between $ftime and $ttime
	$nostr
	$namestr
	$emailstr
	$hkidstr
	ORDER BY id DESC Limit $Offset, $ListNum";

	$Query1="SELECT 
	count(*) as total
	 from $table
	 WHERE 
	purchase_date	between $ftime and $ttime
	$nostr
	$namestr
	$emailstr
	$hkidstr
	ORDER BY id DESC";

}else{
	if ($all=='all') $region='';
	else $region="where	purchase_date	>= ".$_SESSION['lastlogin']; 

	$Query="SELECT 
	 id, $listfield1, $listfield2, $listfield3,$listfield4, $listfield5, $listfield6,recipient_tel,order_status,delivery_date,billing_country,subtotal,shippingfee from $table
	 $region 
	 ORDER BY id DESC  Limit $Offset, $ListNum";

	$Query1 = "select count(*) as total from $table $region";
	
}


$list = myQuery($Query);


$CountRecord = myQuery($Query1);

$CountNum = $CountRecord[0][0];
$Url="&all=$all&fy=$fy&fm=$fm&fd=$fd&ty=$ty&tm=$tm&td=$td&no=$no&firstname=$firstname&lastname=$lastname&member_email=$member_email&hkid=$hkid&action=$action";	

function options($from,$to,$select) {
	$html='';
	for ($i=$from;$i<=$to;$i++) {
		if ($i==$select) $sel=' selected';
			else $sel='';
		$html.='<option value="'.$i.'"'.$sel.'>'.$i.'</option>';
	}
	return $html;	
}
	
	if (null==$fd) $fd=date('d');
	if (null==$fm) $fm=date('m');
	if (null==$fy) $fy=date('Y');
	if (null==$td) $td=date('d');
	if (null==$tm) $tm=date('m');
	if (null==$ty) $ty=date('Y');


?>
<script language="javascript">
<!--
function openWindow(url,para) {
	window.open (url,"",para);
}
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
-->
</script>
<style type="text/css">
<!--
.style1 {
	color: #FF0000;
	font-weight: bold;
}
-->
</style>

<table width="90%" border="0" align="center">
  <tr> 
    <td width="70%">
	
	
	
	
	
	
	
<FORM ACTION="" METHOD=GET name="ask" id="ask">		  
		  
<input name="action" type="hidden" value="search">
		  
	
	<table border="0">
        <tr bgcolor="#CCCCCC"> 
          <td><div align="right">Order Date (DD-MM-YYYY)</div></td>
          <td>Name</td>
        </tr>
        <tr> 
          <td><div align="right">From :
            <select name="fd">
					<?=options(1,31,$fd)?>
            </select> 
              -
              <select name="fm">
					<?=options(1,12,$fm)?>
              </select>
              -
              <select name="fy">
					<?=options(2004,2013,$fy)?>
              </select>
          </div></td>
          <td><input name="firstname" type="text" class="text" id="firstname">
            (First Name)</td>
        </tr>
        <tr> 
          <td><div align="right">To :
            <select name="td">
				<?=options(1,31,$td)?>
            </select>
  -
  <select name="tm">
<?=options(1,12,$tm)?>
  </select>
  -
  <select name="ty">
<?=options(2004,2013,$ty)?>
  </select>
          </div></td>
          <td><input name="lastname" type="text" class="text" id="lastname">
            (Last Name) </td>
        </tr>
        <tr>
          <td><div align="right">Order Number : </div></td>
          <td><input  name="no" type="text" class="text" id="no"></td>
        </tr>
        <tr>
          <td><div align="right"> HK ID Number : </div></td>
          <td><input  name="hkid" type="text" class="text" id="hkid2"></td>
        </tr>
        <tr>
          <td><div align="right">Email Address : </div></td>
          <td>            <input  name="member_email" type="text" class="text" id="member_email2" size="35"></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input name="Submit" type="submit" class="button" onClick="MM_validateForm('no','','NinRange0:99999');return document.MM_returnValue" value="  Go  "> 
          <input name="listall" type="button" class="button" id="listall" onClick="MM_goToURL('parent','<?=$PHP_SELF?>?all=all');MM_returnValue"  value="List All"></td>
        </tr>
      </table>
</FORM>	  	  
	  
    </td><td align="right"><a href="../../report/export_order.php?fd=<?=$fd?>&fm=<?=$fm?>&fy=<?=$fy?>&td=<?=$td?>&tm=<?=$tm?>&ty=<?=$ty?>&lastname=<?=urlencode($lastname)?>&firstname=<?=urlencode($firstname)?>&action=<?=$action?>&no=<?=$no?>&hkid=<?=$hkid?>&email=<?=$email?>"><img src="../../images/excel.gif" border="0"></a>
</td>
  </tr>
</table>
<em>Order marked with </em><span class="style1">* </span><em>is paid by VISA. </em><br>
<? print(splitPage($Url,$CountNum)); ?>  

        <table width="100%" border="0" cellspacing="1" cellpadding="1" class="tb">
          <tr class="tbheader"> 
            <td width="10%">Order No.</td>
			<td width="10%">Customer</td>
			<td width="20%">Telephone/Email</td>
			<td width="15%">Order Date</td>
			<td width="15%">Order/Shipping Status</td>
			<td width="10%">Total Amount</td>
			<td width="10%">Country</td>
			<td width="10%"></td>
          </tr>


<?
if ($list){
	for ($i=0;$i<count($list);$i++){?>
          <tr class="tbcontent1"> 
            <td><?print(str_pad($list[$i][1],5,'0',STR_PAD_LEFT))?> <?=($list[$i][2]=='VISA')?'<font color="#ff0000">*</font>':''?></td>
			<td><?print($list[$i][3])?></td>
			<td><?print($list[$i][7])?><br><a href="mailto:<?=$list[$i][4]?>"><?=$list[$i][4]?></a></td>
			<td><? print(date('Y-m-d H:i:s',$list[$i][5]))?></td>
			<td><?=$list[$i][8].'<br>'.$list[$i][6]?><? echo ($list[$i][6]=='Delivered')?' ('.date('Y-m-d',$list[$i][9]).')':'';?></td>
			<td><?print(Number_format($list[$i][11]+$list[$i][12],2))?></td>
			<td><? print($list[$i]['10']) ?></td>
			<td align=center bgcolor="ECECEC">
			<a href="edit.php?id=<?print($list[$i][0])?>&Strat_Num=<?=$Start_Num?>"><img src="<?print($rela_path)?>images/bt_edit.gif" border="0" alt="Edit"></a>
			<a href="enquire.php?id=<?print($list[$i][0])?>&Strat_Num=<?=$Start_Num?>"><img src="<?print($rela_path)?>images/bt_enquiry.gif" border="0" Alt="Enquire"></a>
			<a href="javascript:openWindow('../../inc/resend.php?id=<?print($list[$i][0])?>&no=<?print($list[$i][1])?>&Strat_Num=<?=$Start_Num?>','width=400 height=180');"><img src="<?print($rela_path)?>images/email.gif" border="0" Alt="Resend e-Receipt"></a></td>
          </tr>
<?
	}	
}?>


</table>

<?print(splitPage($Url,$CountNum))?>
